package com.TEight.Forum.service.impl;
import java.util.List;

import com.TEight.Forum.dao.ArticleDao;
import com.TEight.Forum.dao.impl.ArticleDaoImpl;
import com.TEight.Forum.pojo.Article;
import com.TEight.Forum.pojo.PageBean;
import com.TEight.Forum.service.ArticleService;
public class ArticleServiceImpl implements ArticleService{
	private ArticleDao articleDao = new ArticleDaoImpl();
	@Override
	 public PageBean<Article> getArticleListByPage(String categoryId, String pageno, String pagerStartIndex) {
        PageBean<Article> pageBean = new PageBean<>();
        pageBean.setTotal(articleDao.getTotal(categoryId));
        pageBean.setPagerNum(Integer.parseInt(pageno));	

        int startIndex = 0;		
        if(pagerStartIndex != null) {
            startIndex = Integer.parseInt(pagerStartIndex);
        }
        pageBean.setPagerIndex((startIndex + pageBean.getPagerNum()) / pageBean.getPagerNum());

        int pages = pageBean.getTotal() % pageBean.getPagerNum() == 0 ?
                pageBean.getTotal() / pageBean.getPagerNum() : pageBean.getTotal() / pageBean.getPagerNum() + 1;
        pageBean.setPages(pages);

        pageBean.setList(articleDao.getListByPage(categoryId, pageBean.getPagerIndex(), pageBean.getPagerNum()));
        return pageBean;
    }
	@Override
	public List<Article> getTitleList(String title) {
		return articleDao.getTitleList(title);
	}
	@Override
	public List<Article> getTitleAllList() {
		// TODO Auto-generated method stub
		return articleDao.getTitleAllList();
	}

	@Override
	public Article getArticleDetails(int articleId) {
		// TODO Auto-generated method stub
		return articleDao.getArticleDetails(articleId);
	}

	@Override
	public List<Article> getAllTitle(int userId) {
		// TODO Auto-generated method stub
		return articleDao.getAllTitle(userId);
	}
    public List<Article> getHotTitleList(int num){
    	return articleDao.getHotTitleList(num);
    }

	@Override
	public void addClickNum(int articleId) {
		// TODO Auto-generated method stub
		articleDao.addClickNum(articleId);
	}

	@Override
	public List<Article> getRandomTitle(int num,int limitNum) {
		// TODO Auto-generated method stub
		return articleDao.getRandomTitle(num,limitNum);
	}


	@Override
	public void articleAdd(Article article) {
		// TODO Auto-generated method stub
		articleDao.articleAdd(article);
	}

	@Override
	 public PageBean<Article> getArticleListByPage(String pageno, String pagerStartIndex) {

        PageBean<Article> pageBean = new PageBean<>();
        pageBean.setTotal(articleDao.getTotal());
        pageBean.setPagerNum(Integer.parseInt(pageno));	

        int startIndex = 0;		
        if(pagerStartIndex != null) {
            startIndex = Integer.parseInt(pagerStartIndex);
        }
        pageBean.setPagerIndex((startIndex + pageBean.getPagerNum()) / pageBean.getPagerNum());
        int pages = pageBean.getTotal() % pageBean.getPagerNum() == 0 ?
                pageBean.getTotal() / pageBean.getPagerNum() : pageBean.getTotal() / pageBean.getPagerNum() + 1;
        pageBean.setPages(pages);
        pageBean.setList(articleDao.getListByPage(pageBean.getPagerIndex(), pageBean.getPagerNum()));
        return pageBean;
    }

	@Override
	public void delete(String articleId) {
		articleDao.delete(articleId);
	}
}
